/* simple_thread.c

   A simple POSIX threads example: create a thread, and then join with it.

  tips: static作用
  https://www.runoob.com/w3cnote/c-static-effect.html

  1.隐藏：当我们同时编译多个文件时，所有未加‘static’前缀的全局变量和函数都具有全局可见性。
  2.储在静态数据区的变量会在程序刚开始运行时就完成初始化，也是唯一的一次初始化。
    共有两种变量存储在静态存储区：全局变量和'static'变量，只不过和全局变量比起来，'static'可以控制变量的可见范围
  3.static 的第三个作用是默认初始化为 0
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void *threadFunc(void *arg) {
  char *s = (char *)arg;

  printf("threadFunc: %s", s);

  return (void *)strlen(s);
}

int main(int argc, char *argv[]) {
  pthread_t t1;
  void *res;
  int s;

  s = pthread_create(&t1, NULL, threadFunc, "Hello world\n");
  if (s != 0) {
    fprintf(stderr, "pthread_create: %s\n", strerror(s));
    exit(EXIT_FAILURE);
  }

  printf("Message from main()\n");
  s = pthread_join(t1, &res);
  if (s != 0) {
    fprintf(stderr, "pthread_create: %s\n", strerror(s));
    exit(EXIT_FAILURE);
  }

  printf("Thread returned %ld\n", (long)res);

  exit(EXIT_SUCCESS);
}